package com.fscp.diner.mapper;

import com.fscp.commons.dto.DinerDTO;
import com.fscp.commons.model.pojo.Diner;
import com.fscp.commons.model.vo.ShortDinerInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 食客 Mapper
 */
public interface DinerMapper {

    /**
     * 根据 phone 查询食客信息
     * 这里 is_valid 需要注意 mybatis 驼峰映射处理; 否则无法映射
     *
     * @param phone
     * @return
     */
    @Select("select id, username, phone, email, is_valid " +
            "from t_diner where phone = #{phone}")
    Diner selectDinerByPhone(@Param("phone") String phone);

    /**
     * 根据用户名查询食客信息
     *
     * @param username
     * @return
     */
    @Select("select id, username, phone, email, is_valid " +
            "from t_diner where username = #{username}")
    Diner selectDinerByUsername(@Param("username") String username);

    /**
     * 新增食客信息
     *
     * @param dinerDTO
     * @return
     */
    @Insert("insert into " +
            "t_diner (username, password, phone, roles, is_valid, create_date, update_date) " +
            "values (#{username}, #{password}, #{phone}, \"ROLE_USER\", 1, now(), now())")
    int saveDiner(DinerDTO dinerDTO);

    /**
     * 根据 ID 集合查询多个食客信息
     * 这里使用了一个 foreach 的 script
     *
     * @param ids
     * @return
     */
    @Select("<script> " +
            " select id, nickname, avatar_url from t_diner " +
            " where is_valid = 1 and id in " +
            " <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\"> " +
            "   #{id} " +
            " </foreach> " +
            " </script>")
    List<ShortDinerInfo> findByIds(@Param("ids") String[] ids);

}
